﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.Model;
using System.Data;
using RoadTeam.Enums;
using RoadTeam.SysInterface;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{
    public class CsDoorTypeBll
    {
        private static object o = new object();

        public DataTable GetDoorTypeData(String OrganIdentifier)
        {
            ReflashData(OrganIdentifier);
            SqlWhere where = new SqlWhere();
            where.Info = new CsDoorType() { OrganIdentifier = OrganIdentifier };
            //where.SelectColumn = @" DoorTypeIdentifier,DoorTypeName,DoorTypeCode,OrganIdentifier ";
            return CsDoorType.Select(where, GlobalConfig.ConnectionString);
        }

        /// <summary>
        /// 同步服务器中的户型信息
        /// </summary>
        /// <param name="OrganIdentifier">对接系统中的机构标识</param>
        public void ReflashData(String OrganIdentifier)
        {
            String OrganID = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
            DataTable dt_doortype = GetEmptyTable();
            DataRow dr_doortype;
            lock (o)
            {
                DataTable  dt = FactoryClass.CreateCsDoorType().GetDoorTypeNotNullTable(OrganID);
                //OrganIdentifier
                DataTable dt_Temp=GetDoorData(OrganIdentifier);
                DataRow dr_Temp;
                DataRow [] dr_Temps;
                foreach (DataRow dr in dt.Rows)
                {
                    dr_Temps = dt_Temp.Select(" DoorTypeIdentifier='" + dr["App_Identifier"].ToString() + "' ");
                    if (dr_Temps.Count() == 0) continue;
                    dr_Temp = dr_Temps[0];
                    dr_Temp["DoorTypeName"] = dr["DoorTypeName"];
                    dr_Temp["DoorTypeCode"] = dr["DoorTypeCode"];
                }

                CsDoorType.Update(dt_Temp, GlobalConfig.ConnectionString);


                
                
                
                //更新还没在App数据库中的数据
                 dt=  FactoryClass.CreateCsDoorType().GetDoorTypeTable(OrganID);
              foreach (DataRow dr in dt.Rows)
              {
                  dr[2] = CreateIdentifier.GetIdentifier(); 
                  dr_doortype=dt_doortype.NewRow ();
                dr_doortype["DoorTypeIdentifier"] = dr[2];
                dr_doortype["DoorTypeName"] = dr[0];
                dr_doortype["DoorTypeCode"] = dr[1];
                dr_doortype["OrganIdentifier"] = FactoryClass.CreateSmOrgan().GetIdentitiferByID(dr[3].ToString());
                dt_doortype.Rows.Add(dr_doortype);
              }
                FactoryClass.CreateCsDoorType().UpdateDoorTypeTable(dt);




            }
            CsDoorType.InsertDatatable(dt_doortype, GlobalConfig.ConnectionString);
        }

        public DataTable GetEmptyTable()
        {
            SqlWhere where = new SqlWhere();
            where.SelectColumn = @" DoorTypeIdentifier,DoorTypeName,DoorTypeCode,OrganIdentifier ";
            where.Info = new Model.CsDoorType() { DoorTypeIdentifier="-1" };
            return CsDoorType.Select(where, GlobalConfig.ConnectionString);
        }

        public DataTable GetDoorData(String OrganIdentifier)
        {
            SqlWhere where = new SqlWhere();
            where.SelectColumn = @" DoorTypeIdentifier,DoorTypeName,DoorTypeCode,OrganIdentifier ";
            where.Info = new Model.CsDoorType() { OrganIdentifier = OrganIdentifier };
            return CsDoorType.Select(where, GlobalConfig.ConnectionString);
        }


    }
}
